# -*- coding: utf-8 -*-
'''
Created on 11-10-2012

@author: LONG HOANG GIANG
'''
import sys, os
from urlparse import urljoin
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
from CrawlerLib2 import commonlib, html2text
from Model.storymodelv2 import StoryModel
import re

BASE_URL = 'http://vnthuquan.net/truyen/'


class VNThuQuan():
    
    _surl = ''
    _chapters = []
    
    def __init__(self, sUrl):
        self._surl = sUrl
        if sUrl != '': self.load_chapters()
        
    def load_chapters(self):
        chapters = commonlib.pickle_load('vnthuquan_store_chapters', __file__)
        if chapters == None:
            chapters = []
            tree = commonlib.loadweb(self._surl).build_tree()
            if tree == None: return
            for item in tree.xpath("//td[.='Mục Lục']/../../following-sibling::*[1]/tr//acronym/a".decode('utf-8')):
                href = item.get('href', '')
                if href == '': continue
                href = urljoin(BASE_URL, href)
                title = commonlib.stringify(item)
                chapters.append({'title': title, 'url': href})
            commonlib.pickle_dump('vnthuquan_store_chapters', chapters, __file__)
        self._chapters = chapters
        
    def load_content(self, url):
        
        tree = commonlib.loadweb(url).build_tree()
        contentNode = tree.xpath("//div[@id='fontchu' and @class='truyen_text']")
        content = html2text.html2text(commonlib.Etree.tostring(contentNode[0]), BASE_URL, download=commonlib.downloadImage, path='/hehe/')
#        content = re.sub(r"\n", "<br />", content)
        return content
    
    def process(self):
        
        adapter = StoryModel(DATABASE_PATH)
        adapter.open()
        for chapter in self._chapters:
            content = self.load_content(chapter['url'])
            cid = adapter.addChapter(unicode(commonlib.toUpper(chapter['title'])))
            adapter.addDetail(cid, content)
            self.printf(chapter['title'], content)
        adapter.close()
        
    def printf(self, title, content):
        
        print '#################################'
        print title
        print '#################################'
        print content
        print '---------------------------------'
            
    
if __name__ == '__main__':
    
    DATABASE_PATH = 'c:/Users/LONG HOANG GIANG/Desktop/story'
    
    
    vntq = VNThuQuan('http://vnthuquan.net/truyen/truyen.aspx?tid=2qtqv3m3237nnnvn1n2n31n343tq83a3q3m3237nqn')
    print vntq.load_content('http://vnthuquan.net/truyen/truyen.aspx?tid=2qtqv3m3237nnnvn1n2n31n343tq83a3q3m3237nqn')
    
    
    
    print '> Finished'
    os._exit(1)